const util = require('../../../utils/util.js')
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: {
      searchCount: false,
      current: 1,
      size: 10,
      ascs: '',
      //升序字段
      descs: ''
    },
    parameter: {},
    loadmore: true,
    articleListBanner: [],
    articleList: [],
    articleCategoryList: [],
    tabCur: -1,
    categoryId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.initPage().then(res => {
      //查出banner文章
      this.articlePageBanner({
        isBanner: '1'
      })
      //查出热门文章
      this.getHot()
      //查出文章分类
      this.articleCategoryPage()
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.articlePage();
    }
  },

  articlePageBanner(parameter) {
    app.api.articlePage(Object.assign({}, {
      searchCount: false,
      current: 1,
      size: 5,
      ascs: '',
      //升序字段
      descs: ''
    }, util.filterForm(parameter))).then(res => {
      let articleListBanner = res.data.records
      this.setData({
        articleListBanner: articleListBanner
      })
    });
  },
  articlePage() {
    app.api.articlePage(Object.assign({}, this.data.page, util.filterForm(this.data.parameter))).then(res => {
      let articleList = res.data.records;
      this.setData({
        articleList: [...this.data.articleList, ...articleList]
      })
      if (articleList.length < this.data.page.size) {
        this.setData({
          loadmore: false
        })
      }
    });
  },
  articleCategoryPage() {
    app.api.articleCategoryPage({
      searchCount: false,
      current: 1,
      size: 10,
      ascs: '',
      //升序字段
      descs: ''
    }).then(res => {
      let articleCategoryList = res.data.records
      this.setData({
        articleCategoryList: articleCategoryList
      })
    });
  },
  getHot(){
    this.setData({
      tabCur: -1,
      articleList: [],
      loadmore: true,
      ['page.current']: 1,
      ['parameter.categoryId']: null,
      ['parameter.isHot']: '1'
    })
    this.articlePage()
  },
  tabSelect(e){
    this.setData({
      tabCur: e.currentTarget.dataset.index,
      articleList: [],
      loadmore: true,
      ['page.current']: 1,
      ['parameter.categoryId']: e.currentTarget.dataset.id,
      ['parameter.isHot']: null
    })
    this.articlePage()
  },
  jumpPage(e) {
    wx.navigateTo({
      url: '/pages/article/article-info/index?id='+e.currentTarget.dataset.id
    });
  }
})